//
//  VisitorView.swift
//  WeiBlog
//
//  Created by xiang on 15/10/15.
//  Copyright © 2015年 xiang. All rights reserved.
//

import UIKit
import SnapKit
// MARK:协议
protocol VisitorViewDelegate:NSObjectProtocol{
    
    func loginToModal()
    func registerToModal()
}

class VisitorView: UIView {

    //为什么我定义了这个,用VisitorView()初始化的时候,也会调用这个函数
//    override init(frame: CGRect) {
//        super.init(frame: frame)
//        backgroundColor = UIColor.redColor()
//        print("ss")
//    }
    

    var delegate:VisitorViewDelegate?
    // MARK:初始化方法
    init(){
        super.init(frame: UIScreen.mainScreen().bounds)
        backgroundColor = UIColor(white: 237.0/255.0, alpha: 1)
        addSubviews()
        autolayout()
        addAnimation()
    }
    
    // MARK:懒加载子控件
    /// 旋转图片
    private lazy var rotateImageView:UIImageView = {
        
        let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        return imageView
        
        }()
    /// 遮罩图片
    private lazy var coverImageView:UIImageView = {
        
        let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        return imageView
        
        }()
    ///小房子
    private lazy var houseImageView:UIImageView = {
        
        let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        
        return imageView
        
        }()
    /// 描述文字
    private lazy var descLabel:UILabel = {
        
        let label = UILabel()
        
        label.text = "关注一些人，回这里看看有什么惊喜"
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(13)
        label.numberOfLines = 0
        label.textAlignment = .Center
//        label.sizeToFit()
        return label
        
        }()
    
    /// 注册按钮
    private lazy var registerBtn:UIButton = UIButton(title: "注册", fontSize: nil, color: UIColor.orangeColor(), bgImageName: "common_button_white_disable", imageName: nil, actionName: "login", target: self)

    /// 登录按钮(不用封装,便于比较两种方法)
    private lazy var loginBtn:UIButton = {
        
        let button = UIButton()
        
        //        button.setImage(UIImage(named: <#T##String#>), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        button.addTarget(self, action: Selector("login"), forControlEvents: UIControlEvents.TouchUpInside)
        return button
        }()
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

// MARK:添加子控件和自动布局

extension VisitorView
{
///  添加控件
    private func addSubviews() {
        
        //
        addSubview(rotateImageView)
        addSubview(coverImageView)
        addSubview(houseImageView)
        addSubview(descLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
    }
    
//系统方法布局,注释在最下面
    
///  添加布局(用Snapkit)
    private func autolayout(){
        //旋转图片的布局
        rotateImageView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-60)
        }
        //遮罩图片的布局
        coverImageView.snp_makeConstraints { (make) -> Void in
            make.width.equalTo(self.snp_width)
            make.height.equalTo(self.snp_height).offset(-200)
        }
        //小房子的布局
        houseImageView.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(rotateImageView.snp_center)
        }
        //描述文字
        descLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(rotateImageView.snp_bottom).offset(20)
            make.centerX.equalTo(rotateImageView.snp_centerX)
            make.width.equalTo(300)
        }
        //注册按钮
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(descLabel.snp_bottom).offset(20)
            make.left.equalTo(descLabel.snp_left)
            make.width.equalTo(140)
            make.height.equalTo(40)
        }
        //登录按钮
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(descLabel.snp_bottom).offset(20)
            make.right.equalTo(descLabel.snp_right)
            make.width.equalTo(140)
            make.height.equalTo(40)
        }
    }
    
///  添加动画
    private func addAnimation(){
        
        let anim = CABasicAnimation()
        
        anim.keyPath = "transform.rotation" //设置动画的属性
        
        anim.toValue = 2 * M_PI
        
        anim.removedOnCompletion = false //可以让动画一直播放
        
        anim.repeatCount = MAXFLOAT
        
        anim.duration = 20
        
        rotateImageView.layer.addAnimation(anim, forKey: nil)//动画是添加在图层上的
    }

}

// MARK:提供接口,当控制器不同显示不同信息
extension VisitorView
{
    func setVisitView(imageName:String,desc:String)
    {
        //        "visitordiscover_image_message"
        houseImageView.image = UIImage(named: imageName)
        descLabel.text = desc
        
        //隐藏旋转图片
        rotateImageView.hidden = true
        //隐藏遮罩图片
        sendSubviewToBack(coverImageView)
    }
}

// MARK:点击事件
extension VisitorView
{
    @objc private func login(){
        
        delegate?.loginToModal()
    }
    
    @objc private func register(){
        
        delegate?.registerToModal()
    }
}

// MARK:用系统自带的方法自动布局
extension VisitorView
{
//    //这个属性是让自动布局有效,默认是true,表示只能通过设置frame
//    //禁用Autoresizing
//    for view in subviews{
//    view.translatesAutoresizingMaskIntoConstraints = false
//    }
//    
//    /**
//    view1.attr1 = view2.attr2 * multiplier + constant
//    第一个控件的某个属性 等于 第二个空间的某个属性*倍数 + 偏移量
//    */
//    //旋转图片的布局
//    addConstraint(NSLayoutConstraint(item: rotateImageView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
//    addConstraint(NSLayoutConstraint(item: rotateImageView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
//    
//    //遮罩图片的布局
//    addConstraint(NSLayoutConstraint(item: coverImageView, attribute: .Width, relatedBy: .Equal, toItem: self, attribute: .Width, multiplier: 1.0, constant: 0))
//    addConstraint(NSLayoutConstraint(item: coverImageView, attribute: .Height, relatedBy: .Equal, toItem: self, attribute: .Height, multiplier: 1.0, constant: -200))
//    
//    //小房子的布局(我们可以将房子和旋转图片看做一个整体,所以小房子以旋转图片作为参考)
//    addConstraint(NSLayoutConstraint(item: houseImageView, attribute: .CenterX, relatedBy: .Equal, toItem: rotateImageView, attribute: .CenterX, multiplier: 1.0, constant: 0))
//    addConstraint(NSLayoutConstraint(item: houseImageView, attribute: .CenterY, relatedBy: .Equal, toItem: rotateImageView, attribute: .CenterY, multiplier: 1.0, constant: 0))
//    
//    //描述文字
//    addConstraint(NSLayoutConstraint(item: descLabel, attribute: .Top, relatedBy: .Equal, toItem: rotateImageView, attribute: .Bottom, multiplier: 1.0, constant: 20))
//    addConstraint(NSLayoutConstraint(item: descLabel, attribute: .CenterX, relatedBy: .Equal, toItem: rotateImageView, attribute: .CenterX, multiplier: 1.0, constant: 0))
//    addConstraint(NSLayoutConstraint(item: descLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0, constant: 300))
//    
//    
//    //注册按钮
//    addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: descLabel, attribute: .Bottom, multiplier: 1.0, constant: 20))
//    addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Left, relatedBy: .Equal, toItem: descLabel, attribute: .Left, multiplier: 1.0, constant: 0))
//    addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0, constant: 140))
//    addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0, constant: 40))
//    
//    //登录按钮
//    addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: descLabel, attribute: .Bottom, multiplier: 1.0, constant: 20))
//    addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Right, relatedBy: .Equal, toItem: descLabel, attribute: .Right, multiplier: 1.0, constant: 0))
//    addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0, constant: 140))
//    addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 0, constant: 40))

}
